﻿using MCUPic.Core;
using MCUPic.Core.Functions.BulkConvert;
using PReactive.Data;
using PReactive.Events;
using PReactive.Mvvm;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MCUPic.Views
{
    public partial class MainView : Form, IViewBind
    {
        public object DataContext { get; set; }

        public MainView()
        {
            InitializeComponent();
            picGroup.MouseDown += PicGroup_MouseDown;
            this.Load += MainView_Load;
            textBox1.KeyPress += TextBox_KeyPress;
            textBox1.LostFocus += TextBox_Outputsize_LostFocus;
            textBox2.KeyPress += TextBox_KeyPress;
            textBox2.LostFocus += TextBox_Outputsize_LostFocus;
            textBox3.KeyPress += TextBox_KeyPress;
            textBox3.LostFocus += TextBox_CutPos_LostFocus;
            textBox4.KeyPress += TextBox_KeyPress;
            textBox4.LostFocus += TextBox_CutPos_LostFocus;
        }


        private void TextBox_Outputsize_LostFocus(object sender, EventArgs e)
        {
            DeletePreZero(sender, e);

            TextBox textBox = sender as TextBox;
            if ((textBox.TextLength == 0) || (((TextBox)sender).Text == "0"))
            {
                textBox.BackColor = Color.Red;
                textBox.ForeColor = Color.White;
                MessageBox.Show("Invalid Param");
                textBox.Focus();
            }
            else
            {
                textBox.BackColor = Color.White;
                textBox.ForeColor = Color.Black;
            }
        }
        private void TextBox_CutPos_LostFocus(object sender, EventArgs e)
        {
            DeletePreZero(sender, e);

            TextBox textBox = sender as TextBox;
            if (textBox.TextLength == 0)
            {
                textBox.Text = "0";
            }
        }
        private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar < '0' || e.KeyChar > '9')
            {
                if (e.KeyChar == '\b')
                    return;

                if (e.KeyChar == '\r')
                {
                    picGroup.Focus();
                    return;
                }

                e.Handled = true;
            }
        }

        private void MainView_Load(object sender, EventArgs e)
        {
            //Console.WriteLine(DataContext?.ToString());
            
        }

        private void PicGroup_MouseDown(object sender, MouseEventArgs e)
        {
            //pictureBox1.Image = Image.FromFile(picGroup.SelectedItem);
        }


        public void SetBind()
        {
            this.BindingData(picGroup, "PicturePaths", "PicPaths", BindingMode.OneWayToSource);
            this.BindingCommand(pButton_BulkConvert, EventType.MouseClick, "BulkConvertCommand");
            this.BindingCommand(picGroup, EventType.MouseDown, "SelectPicCommand", () => { return picGroup.SelectedItem; });
            this.BindingData(txtBox_Path, ControlConst.TEXT, "OutputPath", BindingMode.OneWay);
            this.BindingCommand(btn_SelectOutputPath, EventType.MouseClick, "OutputPathCommand");
            this.BindingData(textBox1, ControlConst.TEXT, "TextBox_OutputWidth", BindingMode.Default);
            this.BindingData(textBox2, ControlConst.TEXT, "TextBox_OutputHeight", BindingMode.Default);
            this.BindingData(textBox3, ControlConst.TEXT, "TextBox_CutPosX", BindingMode.Default);
            this.BindingData(textBox4, ControlConst.TEXT, "TextBox_CutPosY", BindingMode.Default);
            this.BindingData(comboBox1, "SelectedIndex", "PixelFormatIndex", BindingMode.Default);
            this.BindingData(comboBox2, "SelectedIndex", "ScaleMode", BindingMode.Default);
            this.BindingData(checkBox1, ControlConst.CHECKED, "CheckCCode", BindingMode.Default);
            this.BindingData(checkBox2, ControlConst.CHECKED, "CheckHex", BindingMode.Default);
            this.BindingData(checkBox3, ControlConst.CHECKED, "CheckBin", BindingMode.Default);
            this.BindingData(checkBox4, ControlConst.CHECKED, "MSBF", BindingMode.Default);
        }

        private void DeletePreZero(object sender, EventArgs e)
        {
            if (((TextBox)sender).TextLength > 1)
            {
                ((TextBox)sender).Text = ((TextBox)sender).Text.TrimStart('0');
            }
        }

    }
}
